package com.uenpay.im.invokeitems.contacts;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.serializations.QYXUserSerializer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 搜素好友
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class SearchFriendsByPhoneNumInvokeItem extends HttpInvokeItem {

	public class SearchFriendsByPhoneNumInvokeItemResult {
		public int status;
		public String msg;
		public long timeStamp;
		public ArrayList<QYXUserEntity> users;
	}

	public SearchFriendsByPhoneNumInvokeItemResult getOutput() {
		return (SearchFriendsByPhoneNumInvokeItemResult) GetResultObject();
	}

	/**
	 * 搜素好友
	 * @param phoneNum
	 */
	public SearchFriendsByPhoneNumInvokeItem(String phoneNum) {
		String url = APIConfiguration.getBaseUrl() + "Cust/Cust/search?username="
				+ phoneNum + "&" + APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		SearchFriendsByPhoneNumInvokeItemResult result = new SearchFriendsByPhoneNumInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONArray dataJson = json.optJSONArray("data");
			if (dataJson != null) {
				result.users = QYXUserSerializer.deserializeUsers(dataJson);
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}

}
